package com.cloudcc.mobile.presenter;

import java.util.List;

import android.text.TextUtils;

import com.cloudcc.cloudframe.bus.DynamicEventList.SendDynamicEvent;
import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.IServer;
import com.cloudcc.cloudframe.net.RequestListener;
import com.cloudcc.cloudframe.util.ListUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.dao.Att;
import com.cloudcc.mobile.dao.impl.EventTaskEngineImpl;
import com.cloudcc.mobile.entity.AddImageInfo;
import com.cloudcc.mobile.entity.EventTaskEntity;
import com.cloudcc.mobile.entity.addmicropost.AddMicroPostDMany;
import com.cloudcc.mobile.entity.addmicropost.AddMicroPostF;
import com.cloudcc.mobile.entity.map.MapLocation;
import com.cloudcc.mobile.event.EventTaskEventList.SendEventEvent;
import com.google.gson.JsonObject;
import com.litesuits.android.log.Log;

public class EventOrTaskPresenter extends BasePresenter {

	private Att mEventTaskEngine;

	public EventOrTaskPresenter() {
		super();
		mEventTaskEngine = new EventTaskEngineImpl();
	}

	/**
	 * 签到 签到不涉及 插入微贴
	 * 
	 * @param event
	 * @param loc
	 * @param li
	 */
	public void sendEvent(final EventTaskEntity event, RequestListener li) {
		mEventTaskEngine.sendEvent(event, li);
	}

	public void sendEventAndInsertTimeLine(final EventTaskEntity event,
			final MapLocation loc, final List<AddImageInfo> imgs) {
		// 注册 到bus
		EventEngine.register(this);
		mEventTaskEngine.sendEvent(event, new RequestListener() {
			@Override
			public void onSuccess(Object result) {
				JsonObject asJsonObject = ((JsonObject) result).get("data")
						.getAsJsonObject().get("ids").getAsJsonArray().get(0)
						.getAsJsonObject();
				String eventId = asJsonObject.get("id").getAsString();
				Log.d("task", eventId + "id");
				Log.d("onsuccess", ":::" + result);
				// 发送普通
				if (!ListUtils.isEmpty(imgs)) {
					sendImagesDynamic(imgs, event, loc, eventId);
					Log.d("task", "有图片的");
				} else {
					sendCommonDynamic(event, loc, eventId);
					Log.d("task", "没有图片的");
				}
			}

			@Override
			public void onFailure(ErrorInfo error) {
				sendEventResultEvent_z(false, error.getErrorMessage());
			}
		});
	}

	// 签到失败
	private void sendEventResultEvent_z(boolean isOK, String string) {
		Log.d("qiandao", "签到失败填数据");
		SendEventEvent eventResult = new SendEventEvent();
		eventResult.setOk(isOK);
		eventResult.setMessage(string);
		EventEngine.post(eventResult);
	}

	/**
	 * 发送
	 * 
	 * @param isOK
	 */
	private void sendEventResultEvent(boolean isOK) {
		SendEventEvent eventResult = new SendEventEvent();
		eventResult.setOk(isOK);
		EventEngine.post(eventResult);
	}

	/**
	 * 发送成功
	 * 
	 * @param event
	 */
	public void onEventMainThread(SendDynamicEvent event) {
		EventEngine.uregister(this);
		sendEventResultEvent(event.isOk());
	}

	/**
	 * 当完成插入事件的时候 添加插入到微贴
	 * 
	 * @param renwu
	 * @param loc
	 * @param eventId
	 */
	private void sendCommonDynamic(EventTaskEntity renwu, MapLocation loc,
			String eventId) {

		SendDynamicEvent event = new SendDynamicEvent();

		if (renwu == null || TextUtils.isEmpty(eventId)) {
			event.setOk(false);
			event.setMessage("参数错误...");
			EventEngine.post(event);
			return;
		}

		AddMicroPostF addMicroPostF = new AddMicroPostF();
		if (loc != null) {
			addMicroPostF.setLatitude(loc.getLatitude());
			addMicroPostF.setLongitude(loc.getLongitude());
			addMicroPostF.setAddress(loc.getAddress());
			Log.d("maplocat",
					loc.getLatitude() + loc.getLongitude() + loc.getAddress());
		}
		addMicroPostF.setTargetType("record");
		addMicroPostF.setTargetId(TextUtils.isEmpty(renwu.getWhoid()) ? renwu
				.getRelateid() : renwu.getWhoid());// 从签到进去的传 事件id 详情进去的传详情id
		Log.d("task", renwu.getRelateid() + renwu.getWhoid());
		addMicroPostF.setTaskIdOrEventId(eventId);
		addMicroPostF.setBody(renwu.getRemark());
		new DynamicControl().sendCommonDynamic(addMicroPostF,
				IServer.SERVICENAME_FOLLEW);
	}

	/**
	 * 当完成插入事件的时候 添加插入到微贴的图片
	 * 
	 * @param imgs
	 * @param event
	 * @param loc
	 * @param eventId
	 */
	private void sendImagesDynamic(List<AddImageInfo> imgs,
			EventTaskEntity event, MapLocation loc, String eventId) {
		AddMicroPostDMany addMicroPostDMany = new AddMicroPostDMany();
		if (loc != null) {
			addMicroPostDMany.setLatitude(loc.getLatitude());
			addMicroPostDMany.setLongitude(loc.getLongitude());
			addMicroPostDMany.setAddress(loc.getAddress());
		}
		addMicroPostDMany.setBody(event.getRemark());
		addMicroPostDMany
				.setTargetId(TextUtils.isEmpty(event.getWhoid()) ? event
						.getRelateid() : event.getWhoid());// 从签到进去的传 事件id
															// 详情进去的传详情id
		addMicroPostDMany.setTaskIdOrEventId(eventId);
		addMicroPostDMany.setTargetType("record");
		AppContext.ADDMICROPOST = "addMicroPostDMany";
		new DynamicControl().sendPhotoDynamic(imgs, addMicroPostDMany);
	}

}
